package nc.ui.pub.print;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JDialog;
import javax.swing.JFrame;
import nc.bs.logging.Logger;
import nc.ui.ml.NCLangRes;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.UIComboBox;
import nc.ui.pub.beans.util.NCOptionPane;
import nc.ui.pub.print.cache.PrintProfileCache;
import nc.ui.pub.print.cache.PrintTemplateCache;
import nc.ui.pub.print.cache.TemplateSelectVO;
import nc.ui.pub.print.datastruct.PreDownInfo;
import nc.ui.pub.print.datastruct.PrintCellData;
import nc.ui.pub.print.datastruct.PrintData;
import nc.ui.pub.print.datastruct.TemplateInfo;
import nc.ui.pub.print.debug.PrintDebug;
import nc.ui.pub.print.debug.PrintDebugData;
import nc.ui.pub.print.ide.printsetting.PrintSettingModel;
import nc.ui.pub.print.ide.printsetting.PrintSettingPanel;
import nc.ui.pub.print.output.AbstractTemplateOutputTask;
import nc.ui.pub.print.output.AbstractTemplateParser;
import nc.ui.pub.print.output.DefaultDataSourceOrganizer;
import nc.ui.pub.print.output.DefaultTemplateDataAccessor;
import nc.ui.pub.print.output.DefaultTemplateParser;
import nc.ui.pub.print.output.IDataSourceOrganizer;
import nc.ui.pub.print.output.ITemplate;
import nc.ui.pub.print.output.ITemplateDataAccessor;
import nc.ui.pub.print.output.OutputJobUtils;
import nc.ui.pub.print.output.VoucherDataSourceOrganizer;
import nc.ui.pub.print.output.html.HtmlOutputTask;
import nc.ui.pub.print.output.preview.PreviewTask;
import nc.ui.pub.print.page.Doc;
import nc.ui.pub.print.page.DocGroup;
import nc.ui.pub.print.page.DocGroupUtil;
import nc.ui.pub.print.page.DocItem;
import nc.ui.pub.print.version55.log.PrintLog;
import nc.ui.pub.print.version55.print.DynamicDoc;
import nc.ui.pub.print.version55.print.PrintCache;
import nc.ui.pub.print.version55.print.PrintException;
import nc.ui.pub.print.version55.print.ds.OldDsAdapter;
import nc.ui.pub.print.version55.print.merge.CopyConfig;
import nc.ui.pub.print.version55.print.output.OutputDocument;
import nc.ui.pub.print.version55.print.output.excel.ExcelOutputDialog;
import nc.ui.pub.print.version55.print.output.excel.ExcelTask;
import nc.ui.pub.print.version55.print.output.html.HtmlOutputDialog;
import nc.ui.pub.print.version55.print.output.html.HtmlOutputSetting;
import nc.ui.pub.print.version55.print.output.html.HtmlTask;
import nc.ui.pub.print.version55.print.output.paint.pdf.PdfOutputDialog;
import nc.ui.pub.print.version55.print.output.paint.pdf.PdfTask;
import nc.ui.pub.print.version55.print.output.paint.preview.PreviewDialog;
import nc.ui.pub.print.version55.print.output.paint.preview.PreviewFrame;
import nc.ui.pub.print.version55.print.output.paint.preview.PreviewPanel;
import nc.ui.pub.print.version55.print.output.paint.print.PrintOutputSetting;
import nc.ui.pub.print.version55.print.template.Paper;
import nc.ui.pub.print.version55.print.template.Template;
import nc.ui.pub.print.version55.print.template.TemplateAdapter;
import nc.vo.pub.BusinessException;
import nc.vo.pub.print.MergeConfig;
import nc.vo.pub.print.PrintCellVO;
import nc.vo.pub.print.PrintDatasourceVO;
import nc.vo.pub.print.PrintTemplateVO;
import nc.vo.pub.print.PrintTempletmanageHeaderVO;
import nc.vo.pub.print.PrintVO;
import nc.vo.uap.pf.TemplateParaVO;



public class PrintEntry {
	private Container parent = null;

	private IDataSourceOrganizer m_dsOrganizer = new DefaultDataSourceOrganizer();

	private ITemplateDataAccessor m_printDataAccessor = new DefaultTemplateDataAccessor();

	private AbstractTemplateParser m_templateParser = null;

	private AbstractTemplateOutputTask m_outputTask = null;

	private TemplateContainer templates = null;

	private DocumentContainer documents = null;

	private String m_strPreString = null;

	private int batchExportType = 1;

	private IPrintListener printListener = null;

	private PrintOutputSetting developPos;

	private Paper developPaper;

	public PrintEntry(Component parent) {
		this.parent = ((Container)parent);
	}


	public PrintEntry(Container parent, IDataSource dataListener) {
		this.parent = parent;

		this.m_dsOrganizer = new DefaultDataSourceOrganizer();

		if (dataListener != null) {
			this.m_dsOrganizer.addDataSource(dataListener);
		}
	}





	public PrintEntry(Component parent, IDataSource dataListener)
	{
		if (parent != null) {
			this.parent = ((Container)parent);
		}
		this.m_dsOrganizer = new DefaultDataSourceOrganizer();

		if (dataListener != null) {
			this.m_dsOrganizer.addDataSource(dataListener);
		}
	}








	public void setDataSource(IDataSource dataListener)
	{
		if (dataListener != null) {
			this.m_dsOrganizer.addDataSource(dataListener);
		}
	}


	/**
	 * @deprecated
	 */
	 public void setTemplateID(String templateID)
	{
		 setTemplateID(templateID, true);
	}













	 public void setTemplateID(String pkCorp, String funCode, String pkUser, String pkBusiType)
	 {
		 setTemplateID(pkCorp, funCode, pkUser, pkBusiType, null);
	 }










	 public void setTemplateID(String pkCorp, String funCode, String pkUser, String pkBusiType, String nodeKey)
	 {
		 setTemplateID(pkCorp, funCode, pkUser, pkBusiType, nodeKey, "1");
	 }












	 public void setTemplateID(String pkCorp, String funCode, String pkUser, String pkBusiType, String nodeKey, String orgtype)
	 {
		 try
		 {
			 this.m_printDataAccessor.setTemplateID(pkCorp, funCode, pkUser, pkBusiType, nodeKey, orgtype);
		 } catch (Exception e) {
			 Logger.error(e.getMessage(), e);
		 }
	 }



	 /**
	  * @deprecated
	  */
	 public void setTemplateID(String templateID, boolean changeIDArray)
	 {
		 try
		 {
			 this.m_printDataAccessor.setTemplateID(templateID, changeIDArray);
		 } catch (Exception e) {
			 Logger.error(e.getMessage(), e);
			 MessageDialog.showErrorDlg(this.parent, null, 
					 NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000244"));
		 }
	 }

	 public void setTemplateIDs(String[] templateIDs) {
		 try {
			 this.m_printDataAccessor.setTemplateIDs(templateIDs);
		 } catch (Exception e) {
			 Logger.error(e.getMessage(), e);
			 MessageDialog.showErrorDlg(this.parent, null, 
					 NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000244"));
		 }
	 }

	 private PrintTempletmanageHeaderVO[] getTemplates(ITemplateDataAccessor templateDataAccessor) {
		 if ((templateDataAccessor.getTemplateIDs() == null) || (templateDataAccessor.getTemplateIDs().length == 0)) {
			 MessageDialog.showErrorDlg(this.parent, null, NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000246") + "," + NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000243"));
			 return null;
		 }

		 PrintTempletmanageHeaderVO[] templateHeaderVOs = templateDataAccessor.getAllTemplates();

		 return templateHeaderVOs;
	 }


















































































	 public int selectTemplate()
	 {
		 if (this.templates != null) {
			 TemplateSelectVO defaultSelect = PrintProfileCache.getInstance().loadTemplateInfo(this.templates);
			 PrintTemplateSelectDlg2 dlg = new PrintTemplateSelectDlg2(this.parent, this.templates, defaultSelect);
			 if (dlg.showModal() == 1) {
				 return 1;
			 }

			 return 0;
		 }

		 PrintTempletmanageHeaderVO[] templateHeaderVOs = getTemplates(this.m_printDataAccessor);


		 if ((templateHeaderVOs == null) || (templateHeaderVOs.length == 0)) {
			 MessageDialog.showErrorDlg(this.parent, null, NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000247"));
			 return -1;
		 }


		 if (templateHeaderVOs.length == 1) {
			 try {
				 this.m_printDataAccessor.setTemplateID(templateHeaderVOs[0].getCtemplateid(), true);
			 } catch (Exception e) {
				 Logger.error(e.getMessage(), e);
			 }
			 return 1;
		 }


		 TemplateParaVO paraVO = this.m_printDataAccessor.getTemplateSelectPara();
		 String lastSelectedTemplatePk = null;
		 if (paraVO != null) {
			 lastSelectedTemplatePk = PrintProfileCache.getInstance().loadLastSelectedTemplate(paraVO);
		 }

		 String lastSelectedTemplateName = null;
		 String[] templateNames = new String[templateHeaderVOs.length];
		 for (int i = 0; i < templateHeaderVOs.length; i++) {
			 templateNames[i] = templateHeaderVOs[i].getVtemplatename();
			 if (templateHeaderVOs[i].getCtemplateid().trim().equalsIgnoreCase(lastSelectedTemplatePk)) {
				 lastSelectedTemplateName = templateNames[i];
			 }
		 }
		 PrintTemplateSelectDlg selectTemplate = new PrintTemplateSelectDlg(this.parent, NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000248"));
		 selectTemplate.addTempletNames(templateNames, lastSelectedTemplateName);
		 selectTemplate.setLocation(200, 150);
		 selectTemplate.show();
		 int operateFlag = selectTemplate.getOperateFlag();
		 if (operateFlag == 2)
			 return -1;
		 String oldID = this.m_printDataAccessor.getTemplateID();

		 String tempName = selectTemplate.getTempletName();
		 String templateID = null;
		 for (int i = 0; i < templateHeaderVOs.length; i++) {
			 if (templateHeaderVOs[i].getVtemplatename().equals(tempName)) {
				 templateID = templateHeaderVOs[i].getCtemplateid();
				 break;
			 }
		 }

		 if (paraVO != null) {
			 PrintProfileCache.getInstance().storeLastSelectedTemplate(templateID, paraVO);
		 }

		 if (!templateID.equals(oldID)) {
			 try {
				 this.m_printDataAccessor.setTemplateID(templateID, true);
			 }
			 catch (Exception e)
			 {
				 Logger.error(e.getMessage(), e);
			 }
		 }

		 return operateFlag;
	 }









	 private boolean isDialog(Component parentComponent)
			 throws HeadlessException
			 {
		 if (parentComponent == null) {
			 return false;
		 }
		 if ((parentComponent instanceof Dialog)) {
			 return true;
		 }
		 if ((parentComponent instanceof Frame)) {
			 return false;
		 }
		 return isDialog(parentComponent.getParent());
			 }



	 public void preview()
	 {
		 Container container = this.parent;
		 boolean isDialog = false;
		 while (container != null) {
			 if ((container instanceof JDialog)) {
				 isDialog = true;
				 break;
			 }
			 if ((container instanceof JFrame))
				 break;
			 if ((container instanceof JApplet)) {
				 break;
			 }
			 container = container.getParent();
		 }

		 if (isDialog) {
			 previewInDialog(true);
		 } else
			 previewInFrame();
	 }

	 private void previewInFrame() {
		 long curTime = System.currentTimeMillis();

		 if (this.documents != null) {
			 OutputDocument docs = DocGroupUtil.orgOutputDocument(this.documents);

			 if ((docs == null) || (docs.getDocNum() == 0) || (docs.getDoc(0) == null))
			 {
				 return;
			 }

			 Template t = ((DynamicDoc)docs.getDocs().get(0)).getT();
			 CopyConfig cc = new CopyConfig();

			 Paper pageSize = PrintCache.getInstance().loadPaper(PrintCache.getTemplateKey(docs));
			 if (pageSize == null) {
				 pageSize = t.getOutPageSize();
			 }
			 MergeConfig mc = PrintCache.getInstance().loadMergeConfig(PrintCache.getTemplateKey(docs));
			 if (mc == null) {
				 mc = new MergeConfig();
			 }
			 try {
				 PreviewFrame previewFrame = new PreviewFrame(docs, pageSize, mc, cc, getPrintListener());
				 previewFrame.setVisible(true);
			 } catch (PrintException e) {
				 MessageDialog.showErrorDlg(this.parent, NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000056"), e.getMessage());
			 }
		 }
		 else {
			 Template t = TemplateAdapter.translate((DefaultTemplateDataAccessor)this.m_printDataAccessor);
			 OutputDocument docs = DocGroupUtil.orgOutputDocument((DefaultTemplateDataAccessor)this.m_printDataAccessor, this.m_dsOrganizer);

			 Paper pageSize = PrintCache.getInstance().loadPaper(PrintCache.getTemplateKey(docs));
			 if (pageSize == null) {
				 pageSize = t.getOutPageSize();
			 }
			 MergeConfig mc = PrintCache.getInstance().loadMergeConfig(PrintCache.getTemplateKey(docs));
			 if (mc == null) {
				 mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
			 }
			 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);

			 System.out.println(NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000109") + (System.currentTimeMillis() - curTime));
			 curTime = System.currentTimeMillis();
			 try
			 {
				 PreviewFrame previewFrame = new PreviewFrame(docs, pageSize, mc, cc, getPrintListener());
				 previewFrame.setVisible(true);
			 } catch (PrintException e) {
				 PrintLog.error(e);
				 MessageDialog.showErrorDlg(this.parent, NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000056"), e.getMessage());
			 }


			 System.out.println(NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000110") + (System.currentTimeMillis() - curTime));
		 }
	 }






	 public PreviewPanel previewInPanel()
	 {
		 if (this.documents != null) {
			 OutputDocument docs = DocGroupUtil.orgOutputDocument(this.documents);

			 Template t = ((DynamicDoc)docs.getDocs().get(0)).getT();
			 MergeConfig mc = new MergeConfig();
			 CopyConfig cc = new CopyConfig();
			 Paper pageSize = t.getOutPageSize();
			 try
			 {
				 return new PreviewPanel(docs, pageSize, mc, cc, getPrintListener());
			 }
			 catch (PrintException e) {
				 PrintLog.error(e.getMessage(), e);
				 return null;
			 }
		 }

		 Template t = TemplateAdapter.translate((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 OutputDocument docs = DocGroupUtil.orgOutputDocument((DefaultTemplateDataAccessor)this.m_printDataAccessor, this.m_dsOrganizer);

		 Paper pageSize = PrintCache.getInstance().loadPaper(PrintCache.getTemplateKey(docs));
		 if (pageSize == null) {
			 pageSize = t.getOutPageSize();
		 }
		 MergeConfig mc = PrintCache.getInstance().loadMergeConfig(PrintCache.getTemplateKey(docs));
		 if (mc == null) {
			 mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 }
		 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);


















		 try
		 {
			 return new PreviewPanel(docs, pageSize, mc, cc, getPrintListener());
		 }
		 catch (PrintException e) {
			 PrintLog.error(e.getMessage(), e); }
		 return null;
	 }








	 public void previewInDialog(boolean modal)
	 {
		 if (this.documents != null) {
			 OutputDocument docs = DocGroupUtil.orgOutputDocument(this.documents);

			 Template t = ((DynamicDoc)docs.getDocs().get(0)).getT();
			 MergeConfig mc = new MergeConfig();
			 CopyConfig cc = new CopyConfig();
			 Paper pageSize = t.getOutPageSize();

			 PreviewDialog previewDlg = new PreviewDialog(this.parent, docs, pageSize, mc, cc, getPrintListener());
			 previewDlg.setModal(modal);
			 previewDlg.setVisible(true);
		 }
		 else {
			 Template t = TemplateAdapter.translate((DefaultTemplateDataAccessor)this.m_printDataAccessor);

			 OutputDocument docs = DocGroupUtil.orgOutputDocument((DefaultTemplateDataAccessor)this.m_printDataAccessor, this.m_dsOrganizer);

			 Paper pageSize = PrintCache.getInstance().loadPaper(PrintCache.getTemplateKey(docs));
			 if (pageSize == null) {
				 pageSize = t.getOutPageSize();
			 }
			 MergeConfig mc = PrintCache.getInstance().loadMergeConfig(PrintCache.getTemplateKey(docs));
			 if (mc == null) {
				 mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
			 }
			 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);

			 PreviewDialog previewDlg = new PreviewDialog(this.parent, docs, pageSize, mc, cc, getPrintListener());
			 previewDlg.setModal(modal);
			 previewDlg.setVisible(true);
		 }
	 }




	 private PreviewTask getPreviewTask()
	 {
		 String taskName = NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000915");

		 if (this.documents != null) {
			 PreviewTask previewTask = new PreviewTask(this.parent, taskName);
			 previewTask.setPrintListener(getPrintListener());
			 previewTask.setDocs(this.documents);
			 return previewTask;
		 }

		 if ((getTemplateID() == null) || (getTemplateID().trim().length() == 0)) {
			 MessageDialog.showErrorDlg(this.parent, null, NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000242") + "," + NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000243"));
			 return null;
		 }

		 this.m_templateParser = new DefaultTemplateParser(this.m_dsOrganizer, this.m_printDataAccessor);
		 if ((getTemplateInfo() != null) && (getTemplateInfo().getTemplateName() != null)) {
			 taskName = NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000916") + getTemplateInfo().getTemplateName();
		 }


		 PreviewTask previewTask = new PreviewTask(this.parent, taskName, this.m_templateParser);
		 previewTask.setPrintListener(getPrintListener());
		 return previewTask;
	 }








	 public void print()
	 {
		 print(true);
	 }






	 public void printVouchersAsWhole()
	 {
		 outputToPrinter(true, true, true);
	 }




	 public void previewVouchersAsWhole()
	 {
		 preview();
	 }




	 private void organizeVouchersDs()
	 {
		 int perPageLines = getTemplateInfo().getBreaPos();

		 AggDataSource aggDs = new VouchAggDataSource(perPageLines);

		 IDataSource ds = this.m_dsOrganizer.fetchNextDataSource();
		 while (ds != null) {
			 aggDs.addDataSource(ds);
			 ds = this.m_dsOrganizer.fetchNextDataSource();
		 }

		 this.m_dsOrganizer = new DefaultDataSourceOrganizer();
		 this.m_dsOrganizer.addDataSource(aggDs);
	 }




	 public void print(boolean bool)
	 {
		 print(true, true);
	 }










	 public int print(boolean isShowProgressDlg, boolean isShowPrintSettingDlg) {
		 if (!checkPrecondition()) {
			 return -1;
		 }
		 Template t = TemplateAdapter.translate((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 OutputDocument docs = DocGroupUtil.orgOutputDocument((DefaultTemplateDataAccessor)this.m_printDataAccessor, this.m_dsOrganizer);
		 Paper paper = PrintCache.getInstance().loadPaper(PrintCache.getTemplateKey(docs));
		 if (paper == null) {
			 paper = t.getOutPageSize();
		 }

		 PrintOutputSetting pos = PrintCache.getInstance().loadPrintOutputSetting(PrintCache.getTemplateKey(docs));
		 if (pos == null)
			 pos = new PrintOutputSetting();
		 if (isShowPrintSettingDlg) {
			 nc.ui.pub.print.version55.print.output.paint.print.printset.PrintSettingDlg psd = new nc.ui.pub.print.version55.print.output.paint.print.printset.PrintSettingDlg(this.parent, pos, paper);

			 if (psd.showModal() != 1) {
				 return -1;
			 }
			 pos = psd.getPrintSettingModel();
			 PrintCache.getInstance().storePrintOutputSetting(PrintCache.getTemplateKey(docs), pos);
			 paper = psd.getPrinterPaper();
		 }

		 MergeConfig mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 try {
			 nc.ui.pub.print.version55.print.output.paint.print.PrintTask task = new nc.ui.pub.print.version55.print.output.paint.print.PrintTask(this.parent, docs, paper, mc, cc);
			 task.setListener(getPrintListener());
			 task.setPos(pos);
			 if (isShowProgressDlg) {
				 task.setShowProgressor(true);
			 } else
				 task.setShowProgressor(false);
			 task.run();
		 } catch (PrintException e) {
			 PrintLog.error(e.getMessage(), e);
			 MessageDialog.showErrorDlg(this.parent, NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000056"), e.getMessage());
		 }
		 return 0;
	 }

	 public Object[] developPrint(boolean isShowProgressDlg, boolean isShowPrintSettingDlg, boolean hasdata) {
		 Object[] returnObj = new Object[2];
		 
		 Template t = TemplateAdapter.translate((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 OutputDocument docs;
		 if(hasdata){
			 docs = DocGroupUtil.orgOutputDocument(this.documents);
		 } else {
			 docs = DocGroupUtil.orgOutputDocument((DefaultTemplateDataAccessor)this.m_printDataAccessor, this.m_dsOrganizer);
		 }
		 Paper paper = PrintCache.getInstance().loadPaper(PrintCache.getTemplateKey(docs));
		 if (paper == null) {
			 paper = t.getOutPageSize();
		 }
//		 if (developPos == null){
//			 developPos = PrintCache.getInstance().loadPrintOutputSetting(PrintCache.getTemplateKey(docs));
//		 }
//		 if (developPos == null){
//			 developPos = new PrintOutputSetting();
//		 }
		 if (isShowPrintSettingDlg) {

			 PrintOutputSetting pos = PrintCache.getInstance().loadPrintOutputSetting(PrintCache.getTemplateKey(docs));
			 if (pos == null){
				 pos = new PrintOutputSetting();
			 }
			 nc.ui.pub.print.version55.print.output.paint.print.printset.PrintSettingDlg psd = new nc.ui.pub.print.version55.print.output.paint.print.printset.PrintSettingDlg(this.parent, pos, paper);

			 if (psd.showModal() != 1) {
				 return null;
			 }
			 developPos = psd.getPrintSettingModel();
			 PrintCache.getInstance().storePrintOutputSetting(PrintCache.getTemplateKey(docs), developPos);
			 developPaper = psd.getPrinterPaper();
			 returnObj[0] = developPos;
			 returnObj[1] = developPaper;
			 
		 }

		 MergeConfig mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 try {
			 nc.ui.pub.print.version55.print.output.paint.print.PrintTask task = new nc.ui.pub.print.version55.print.output.paint.print.PrintTask(this.parent, docs, developPaper, mc, cc);
			 task.setListener(getPrintListener());
			 task.setPos(developPos);
			 if (isShowProgressDlg) {
				 task.setShowProgressor(true);
			 } else
				 task.setShowProgressor(false);
			 task.run();
		 } catch (PrintException e) {
			 PrintLog.error(e.getMessage(), e);
			 MessageDialog.showErrorDlg(this.parent, NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000056"), e.getMessage());
		 }
		 return returnObj;
	 }

	 
	 
	 public void print2() {
		 outputToPrinter2(true, true);
	 }









	 /**
	  * @deprecated
	  */
	 public boolean beginBatchPrint()
	 {
		 this.m_dsOrganizer.setPrepared(false);


		 int type = setBatchExportType();

		 if (type == -1)
			 return false;
		 return true;
	 }



	 /**
	  * @deprecated
	  */
	 public void endBatchPrint()
	 {
		 if (!checkPrecondition()) {
			 return;
		 }
		 this.m_dsOrganizer.setPrepared(true);


		 if (this.batchExportType == 2) {
			 outputToExcel(null, true);

		 }
		 else if (this.batchExportType == 1)
		 {
			 print(true, true);

		 }
		 else if (this.batchExportType == 0) {
			 preview();
		 }
	 }









	 /**
	  * @deprecated
	  */
	 public boolean beginVoucherBatchPrint()
	 {
		 if (this.m_printDataAccessor.getPrintData().getTemplateInfo().getPagination() != 1) {
			 throw new RuntimeException(NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000240"));
		 }


		 this.m_dsOrganizer = new VoucherDataSourceOrganizer(getTemplateInfo());


		 int type = setBatchExportType();

		 if (type == -1)
			 return false;
		 return true;
	 }



	 /**
	  * @deprecated
	  */
	 public void endVoucherBatchPrint()
	 {
		 ((VoucherDataSourceOrganizer)this.m_dsOrganizer).organizeVouchers();
		 endBatchPrint();
	 }


















	 /**
	  * @deprecated
	  */
	 public void refineVoucherDataSources()
	 {
		 if ((this.m_dsOrganizer != null) && (this.m_dsOrganizer.getDataSourceCount() > 0)) {
			 IDataSourceOrganizer tmpDses = new VoucherDataSourceOrganizer(getTemplateInfo());
			 IDataSource tmpDs = this.m_dsOrganizer.fetchNextDataSource();
			 while (tmpDs != null) {
				 tmpDses.addDataSource(tmpDs);
				 tmpDs = this.m_dsOrganizer.fetchNextDataSource();
			 }

			 ((VoucherDataSourceOrganizer)tmpDses).organizeVouchers();
			 this.m_dsOrganizer = tmpDses;
		 }
	 }


	 /**
	  * @deprecated
	  */
	 public void cancel()
	 {
		 if (this.m_outputTask != null) {
			 this.m_outputTask.cancel();
		 }
	 }



	 public int exportExcelFile()
	 {
		 try
		 {
			 exportExcelFile2(true);
		 } catch (Exception e) {
			 Logger.error(e.getMessage(), e);
		 }
		 return 0;
	 }






	 int exportExcelFile(String fileName)
	 {
		 try
		 {
			 exportExcelFile2(fileName, true);
		 } catch (Exception e) {
			 Logger.error(e.getMessage(), e);
		 }
		 return 0;
	 }



	 public void exportExcelFile2(boolean isAppend)
			 throws Exception
			 {
		 exportExcelFile2(null, isAppend);
			 }





	 public void exportExcelFile2(String fileName, boolean isAppend)
			 throws Exception
			 {
		 if (!checkPrecondition()) {
			 return;
		 }
		 outputToExcel(fileName, isAppend);
			 }

	 public int exportExcelFile(nc.ui.pub.print.version55.print.output.excel.ExcelOutputSetting eos) {
		 if (!checkPrecondition()) {
			 return -1;
		 }
		 Template t = translateTemplate(this.m_printDataAccessor);
		 OutputDocument docs = DocGroupUtil.orgOutputDocument((DefaultTemplateDataAccessor)this.m_printDataAccessor, this.m_dsOrganizer);

		 if (eos == null) {
			 eos = new nc.ui.pub.print.version55.print.output.excel.ExcelOutputSetting();
		 }

		 Paper pageSize = t.getPageSize();

		 MergeConfig mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);

		 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);

		 ExcelTask task = new ExcelTask(this.parent, docs, eos, pageSize, mc, cc);
		 task.setNewThread(false);
		 task.setShowProgressor(false);
		 task.run();
		 return 1;
	 }

	 public int exportExcelFile(ExcelOutputSetting eos) {
		 nc.ui.pub.print.version55.print.output.excel.ExcelOutputSetting neweos = new nc.ui.pub.print.version55.print.output.excel.ExcelOutputSetting();

		 int addPolicy = eos.getAddPolicy();
		 int dsPositionPolicy = eos.getDsPositionPolicy();
		 String exportDestPath = eos.getExportDestPath();
		 String sheetNamePreFix = eos.getSheetNamePreFix();
		 int userAction = eos.getUserAction();

		 neweos.setAddPolicy(addPolicy);
		 neweos.setDsPositionPolicy(dsPositionPolicy);
		 neweos.setExportDestPath(exportDestPath);
		 neweos.setSheetNamePreFix(sheetNamePreFix);
		 neweos.setUserAction(userAction);

		 return exportExcelFile(neweos);
	 }






















	 public int exportPdfFile()
	 {
		 if (!checkPrecondition()) {
			 return -1;
		 }
		 outputToPDF(null, false);
		 return 1;
	 }






	 public int exportPdfFile(String fileName)
	 {
		 if (!checkPrecondition()) {
			 return -1;
		 }
		 outputToPDF(fileName, false);
		 return 1;
	 }






	 public int exportPdfFile(PdfOutputSetting pos2)
	 {
		 if (this.m_templateParser == null) {
			 this.m_templateParser = new DefaultTemplateParser(this.m_dsOrganizer, this.m_printDataAccessor);
		 }

		 Template t = TemplateAdapter.translate((DefaultTemplateDataAccessor)this.m_printDataAccessor);


		 OutputDocument docs = new OutputDocument();
		 IDataSource oldDs = this.m_dsOrganizer.fetchNextDataSource();
		 int i = 0;
		 while (oldDs != null)
		 {
			 docs.addDoc(NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000111", null, new String[] { String.valueOf(i) }), NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000111", null, new String[] { String.valueOf(i) }), t, new OldDsAdapter(oldDs), 1);

			 i++;
			 oldDs = this.m_dsOrganizer.fetchNextDataSource();
		 }


		 nc.ui.pub.print.version55.print.output.paint.pdf.PdfOutputSetting pos = new nc.ui.pub.print.version55.print.output.paint.pdf.PdfOutputSetting();
		 pos.setExportDestPath(pos2.getExportDestPath());
















		 Paper pageSize = t.getPageSize();

		 MergeConfig mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);


		 PdfTask task = new PdfTask(this.parent, docs, pos, pageSize, mc, cc);
		 task.setNewThread(false);
		 task.setShowProgressor(false);
		 task.run();




		 return 1;
	 }
















	 /**
	  * @deprecated
	  */
	 public int exportFtpFile(String fileUrl, int port, String user, String password)
	 {
		 return 0;
	 }




	 /**
	  * @deprecated
	  */
	 public int exportHtmlFile(String fileName)
	 {
		 return 0;
	 }






	 public int exportHtmlFile2(String fileName)
	 {
		 Template t = TemplateAdapter.translate((DefaultTemplateDataAccessor)this.m_printDataAccessor);


		 OutputDocument docs = DocGroupUtil.orgOutputDocument((DefaultTemplateDataAccessor)this.m_printDataAccessor, this.m_dsOrganizer);


		 HtmlOutputSetting hos = PrintCache.getInstance().loadHtmlOutputSetting(PrintCache.getTemplateKey(docs));

		 HtmlOutputDialog exportSettingDlg = new HtmlOutputDialog(this.parent, docs.getDocNum() > 1);
		 exportSettingDlg.setSize(new Dimension(680, 220));

		 if (exportSettingDlg.showModal() == 2) {
			 return -1;
		 }
		 hos = exportSettingDlg.getOutputSetting();
		 if (hos == null) {
			 return -1;
		 }

		 PrintCache.getInstance().storeHtmlOutputSetting(PrintCache.getTemplateKey(docs), hos);

		 Paper pageSize = t.getPageSize();

		 MergeConfig mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);

		 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);

		 HtmlTask task = new HtmlTask(this.parent, docs, hos, pageSize, mc, cc);
		 task.run();

		 return 0;
	 }






	 public int exportHtmlFile2(OutputStream os)
	 {
		 if (this.m_templateParser == null) {
			 this.m_templateParser = new DefaultTemplateParser(this.m_dsOrganizer, this.m_printDataAccessor);
		 }
		 this.m_outputTask = new HtmlOutputTask(this.parent, NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000112"), this.m_templateParser, os);
		 ((HtmlOutputTask)this.m_outputTask).run2();

		 return 0;
	 }






	 public String exportHtmlString()
	 {
		 return null;
	 }






	 /**
	  * @deprecated
	  */
	 public int exportTextFile()
	 {
		 return 0;
	 }






	 /**
	  * @deprecated
	  */
	 int exportTextFile(String splitStr, String fileName, boolean valueStyle)
	 {
		 return 0;
	 }





	 public IDataSource getDataSource()
	 {
		 if (this.m_dsOrganizer == null)
			 return null;
		 return this.m_dsOrganizer.getDataSource();
	 }






	 public PrintSettingModel getPrintSettingModel()
	 {
		 return this.m_printDataAccessor.getPrintSettingModel();
	 }





	 public TemplateInfo getTemplateInfo()
	 {
		 return this.m_printDataAccessor.getPrintData().getTemplateInfo();
	 }





	 public String getTemplateID()
	 {
		 return this.m_printDataAccessor.getTemplateID();
	 }





	 public int getBeginPageNo()
	 {
		 return getPrintSettingModel().getPageFrom();
	 }






	 public int getBreakPos()
	 {
		 if (getTemplateInfo() != null)
			 return getTemplateInfo().getBreaPos();
		 return 0;
	 }



	 /**
	  * @deprecated
	  */
	 public int getCopies()
	 {
		 return getPrintSettingModel().getCopies();
	 }



	 /**
	  * @deprecated
	  */
	 public int getEndPageNo()
	 {
		 return getPrintSettingModel().getPageTo();
	 }





	 /**
	  * @deprecated
	  */
	 public int getPageNumber()
	 {
		 return PrintEnvironment.getInstance().getCurTaskPageIndex();
	 }



	 /**
	  * @deprecated
	  */
	 public int getPagesCount()
	 {
		 return Math.max(0, getEndPageNo() - getBeginPageNo() + 1);
	 }



	 /**
	  * @deprecated
	  */
	 public PageSelectInfo getPageSelectInfo()
	 {
		 PageSelectInfo psi = new PageSelectInfo();
		 psi.setCopies(getPrintSettingModel().getCopies());
		 psi.setFromPage(getPrintSettingModel().getPageFrom());
		 psi.setPageType(getPrintSettingModel().getPageWholeType());
		 psi.setSelectType(getPrintSettingModel().getPageRangeType());
		 if (getTemplateInfo() != null)
			 psi.setStartShowPageNum(getTemplateInfo().getInitStartPageNo());
		 psi.setToPage(getPrintSettingModel().getPageTo());
		 return psi;
	 }



	 /**
	  * @deprecated
	  */
	 public PreDownInfo getPreDownInfo()
	 {
		 return getTemplateInfo().getPreDownInfo();
	 }



	 /**
	  * @deprecated
	  */
	 public boolean isSelectedAll()
	 {
		 return getPageSelectInfo().getSelectType() == 0;
	 }



	 /**
	  * @deprecated
	  */
	 public void setBeginPageNo(int newBeginPageNo)
	 {
		 getPrintSettingModel().setPageFrom(newBeginPageNo);
	 }



	 /**
	  * @deprecated
	  */
	 public void setCopies(int newCopies)
	 {
		 getPrintSettingModel().setCopies(newCopies);
	 }



	 /**
	  * @deprecated
	  */
	 public void setEndPageNo(int newEndPageNo)
	 {
		 getPrintSettingModel().setPageTo(newEndPageNo);
	 }

	 /**
	  * @deprecated
	  */
	 public void setPageSelectInfo(PageSelectInfo newPageSelectInfo)
	 {
		 setPageSelectInfo(newPageSelectInfo, true);
	 }






	 /**
	  * @deprecated
	  */
	 public void setPageSelectInfo(PageSelectInfo psi, boolean changeShowInfo)
	 {
		 getPrintSettingModel().setCopies(psi.getCopies());
		 getPrintSettingModel().setPageFrom(psi.getFromPage());
		 getPrintSettingModel().setPageRangeType(psi.getSelectType());
		 getPrintSettingModel().setPageWholeType(psi.getPageType());
		 getPrintSettingModel().setPageTo(psi.getToPage());
		 if (getTemplateInfo() != null) {
			 getTemplateInfo().setInitStartPageNo(psi.getStartShowPageNum());
		 }
	 }


	 /**
	  * @deprecated
	  */
	 public void setSelectedAll(boolean newSelectedAll)
	 {
		 if (newSelectedAll) {
			 getPrintSettingModel().setPageRangeType(0);
		 } else {
			 getPrintSettingModel().setPageRangeType(2);
		 }
	 }



	 /**
	  * @deprecated
	  */
	 public void setStartPageNum(int startShowPageNum, boolean isAdjustTotalNum)
	 {
		 if (getTemplateInfo() != null) {
			 getTemplateInfo().setInitStartPageNo(startShowPageNum);
		 }
	 }



	 /**
	  * @deprecated
	  */
	 public int getInitStartPageNo()
	 {
		 if (getTemplateInfo() != null)
			 return getTemplateInfo().getInitStartPageNo();
		 return 0;
	 }




	 /**
	  * @deprecated
	  */
	 public void setInitStartPageNo(int initStartPageNo)
	 {
		 setStartPageNum(initStartPageNo, false);
	 }






	 public void setPreString(String prestring)
	 {
		 this.m_strPreString = prestring;
	 }





	 /**
	  * @deprecated
	  */
	 private int setBatchExportType()
	 {
		 ArrayList list = new ArrayList();

		 String[] ss1 = 
			 {
				 NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-001112"), 
				 NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000249"), 
				 NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000250") };


		 list.add(NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000251"));

		 UIComboBox combo = new UIComboBox();
		 for (int i = 0; i < ss1.length; i++) {
			 combo.addItem(ss1[i]);
		 }
		 list.add(combo);

		 if (NCOptionPane.showConfirmDialog(this.parent, list.toArray(new Object[list.size()]), NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000252"), 2, -1) != 0) {
			 this.batchExportType = -1;
		 }
		 else {
			 this.batchExportType = combo.getSelectedIndex();
		 }
		 return this.batchExportType;
	 }




	 /**
	  * @deprecated
	  */
	 public void setPrintCell(PrintCellData[][] cell, TemplateInfo template)
	 {
		 this.m_printDataAccessor.getPrintData().setTemplateInfo(template);
		 this.m_printDataAccessor.getPrintData().setCells(cell);
	 }




	 /**
	  * @deprecated
	  */
	 public boolean setPrintCell(BaseTable sp)
	 {
		 PrintData data = null;
		 try {
			 data = AccessTable.getPrintData(sp);
		 } catch (Exception e) {
			 Logger.error(e.getMessage(), e);
			 MessageDialog.showErrorDlg(this.parent, null, NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000244"));
			 return false;
		 }

		 if (data == null) {
			 return false;
		 }
		 this.m_printDataAccessor.setPrintData(data);

		 return true;
	 }




	 /**
	  * @deprecated
	  */
	 public boolean setPrintCell(PrintData printData)
	 {
		 if (printData == null) {
			 return false;
		 }
		 this.m_printDataAccessor.setPrintData(printData);

		 return true;
	 }







	 public void setPrintListener(IPrintListener printListener)
	 {
		 this.printListener = printListener;
	 }






	 public IPrintListener getPrintListener()
	 {
		 return this.printListener;
	 }





	 /**
	  * @deprecated
	  */
	 public int getPrintType()
	 {
		 return this.m_printDataAccessor.getPrintSettingModel().getPrintType();
	 }



	 /**
	  * @deprecated
	  */
	 public void setPrintType(int printType)
	 {
		 this.m_printDataAccessor.getPrintSettingModel().setPrintType(printType);
	 }





	 private void outputToPrinter(boolean isShowProgressDlg, boolean isShowPrintSettingDlg, boolean isVoucher)
	 {
		 if (this.documents != null)
		 {
			 PrintSettingModel psm = this.m_printDataAccessor.getPrintSettingModel();

			 if (isShowPrintSettingDlg) {
				 nc.ui.pub.print.ide.printsetting.PrintSettingDlg dlg = new nc.ui.pub.print.ide.printsetting.PrintSettingDlg(this.parent, ((DocumentItem)((Document)this.documents.getDocs().get(0)).getItems().get(0)).getTemplate().getPrintData().getTemplateInfo(), 1);
				 if (dlg.showModal() != 1) {
					 return;
				 }
				 psm = dlg.getPrintSettingModel();
			 }

			 if (psm == null) {
				 psm = new PrintSettingModel();
			 }

			 DocGroup docg = DocGroupUtil.organizePrintData(psm, this.documents);


			 this.m_outputTask = new nc.ui.pub.print.output.printer.PrintTask(this.parent, "", docg);
			 this.m_outputTask.setPrintListener(this.printListener);
			 OutputJobUtils.start(this.m_outputTask, isShowProgressDlg, isShowProgressDlg);

			 return;
		 }



		 PrintEnvironment.getInstance().setVoucherModel(isVoucher);

		 boolean isBatchPrint = this.m_dsOrganizer.isBatchPrint();

		 int mode = 1;
		 if (isBatchPrint) {
			 mode = 2;
		 }
		 PrintSettingModel psm = this.m_printDataAccessor.getPrintSettingModel();

		 if (isShowPrintSettingDlg) {
			 nc.ui.pub.print.ide.printsetting.PrintSettingDlg dlg = new nc.ui.pub.print.ide.printsetting.PrintSettingDlg(this.parent, this.m_printDataAccessor.getPrintData().getTemplateInfo(), mode);
			 if (dlg.showModal() != 1) {
				 return;
			 }
			 psm = dlg.getPrintSettingModel();
		 }

		 if (psm == null) {
			 psm = new PrintSettingModel();
		 }
		 this.m_printDataAccessor.setPrintSettingModel(psm);



		 if (PrintDebug.isOpen())
		 {
			 PrintDebugData.getInstance().reset();
		 }

		 this.m_templateParser = new DefaultTemplateParser(this.m_dsOrganizer, this.m_printDataAccessor);








































































		 DocGroup docg = DocGroupUtil.organizePrintData(this.m_printDataAccessor.getPrintSettingModel(), this.m_dsOrganizer, this.m_printDataAccessor);


		 this.m_outputTask = new nc.ui.pub.print.output.printer.PrintTask(this.parent, "", docg);
		 this.m_outputTask.setPrintListener(this.printListener);
		 OutputJobUtils.start(this.m_outputTask, isShowProgressDlg, isShowProgressDlg);
	 }

	 private void outputToPrinter2(boolean isShowProgressDlg, boolean isShowPrintSettingDlg)
	 {
		 PrintEnvironment.getInstance().setVoucherModel(false);

		 boolean isBatchPrint = this.m_dsOrganizer.isBatchPrint();

		 int mode = 1;
		 if (isBatchPrint) {
			 mode = 2;
		 }
		 PrintSettingModel psm = this.m_printDataAccessor.getPrintSettingModel();
		 if (isShowPrintSettingDlg) {
			 nc.ui.pub.print.ide.printsetting.PrintSettingDlg dlg = new nc.ui.pub.print.ide.printsetting.PrintSettingDlg(this.parent, this.m_printDataAccessor.getPrintData().getTemplateInfo(), mode);
			 if (dlg.showModal() != 1) {
				 return;
			 }
			 psm = dlg.getPrintSettingModel();
		 }
		 if (psm == null) {
			 psm = new PrintSettingModel();
		 }
		 this.m_printDataAccessor.setPrintSettingModel(psm);



		 if (PrintDebug.isOpen())
		 {
			 PrintDebugData.getInstance().reset();
		 }

		 IDataSource ds = this.m_dsOrganizer.fetchNextDataSource();
		 Doc doc = new Doc();
		 while (ds != null) {
			 DocItem docItem = new DocItem();
			 docItem.setDs(ds);
			 docItem.setTemplate(this.m_printDataAccessor);
			 doc.addDocItem(docItem);

			 ds = this.m_dsOrganizer.fetchNextDataSource();
		 }


		 this.m_outputTask = new nc.ui.pub.print.output.printer.PrintTask(this.parent, "");

		 this.m_outputTask.setPrintListener(this.printListener);

		 OutputJobUtils.start(this.m_outputTask, isShowProgressDlg, isShowProgressDlg);
	 }







	 private void outputToExcel(String excelFileName, boolean isAppend)
	 {
		 Template t = translateTemplate(this.m_printDataAccessor);













		 OutputDocument docs = DocGroupUtil.orgOutputDocument((DefaultTemplateDataAccessor)this.m_printDataAccessor, this.m_dsOrganizer);




		 nc.ui.pub.print.version55.print.output.excel.ExcelOutputSetting eos = PrintCache.getInstance().loadExcelOutputSetting(PrintCache.getTemplateKey(docs));
		 if ((eos != null) && (excelFileName != null) && (excelFileName.length() > 0)) {
			 eos.setExportDestPath(excelFileName);
		 }
		 ExcelOutputDialog exportSettingDlg = new ExcelOutputDialog(this.parent, docs.getDocNum() > 1, eos);
		 exportSettingDlg.setSize(new Dimension(680, 360));

		 if (exportSettingDlg.showModal() == 2) {
			 return;
		 }
		 eos = exportSettingDlg.getOutputSetting();


		 PrintCache.getInstance().storeExcelOutputSetting(PrintCache.getTemplateKey(docs), eos);


		 Paper pageSize = t.getPageSize();

		 MergeConfig mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);

		 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);

		 ExcelTask task = new ExcelTask(this.parent, docs, eos, pageSize, mc, cc);
		 task.run();
	 }





























	 private void outputToPDF(String fileName, boolean isAppend)
	 {
		 Template t = TemplateAdapter.translate((DefaultTemplateDataAccessor)this.m_printDataAccessor);


		 OutputDocument docs = new OutputDocument();
		 IDataSource oldDs = this.m_dsOrganizer.fetchNextDataSource();
		 int i = 0;
		 while (oldDs != null)
		 {
			 docs.addDoc(NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000111", null, new String[] { String.valueOf(i) }), NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000111", null, new String[] { String.valueOf(i) }), t, new OldDsAdapter(oldDs), 1);

			 i++;
			 oldDs = this.m_dsOrganizer.fetchNextDataSource();
		 }


		 nc.ui.pub.print.version55.print.output.paint.pdf.PdfOutputSetting pos = PrintCache.getInstance().loadPdfOutputSetting(PrintCache.getTemplateKey(docs));

		 PdfOutputDialog exportSettingDlg = new PdfOutputDialog(this.parent, docs.getDocNum() > 1, pos);
		 exportSettingDlg.setSize(new Dimension(680, 220));

		 if (exportSettingDlg.showModal() == 2) {
			 return;
		 }
		 pos = exportSettingDlg.getOutputSetting();

		 if (pos == null) {
			 return;
		 }

		 PrintCache.getInstance().storePdfOutputSetting(PrintCache.getTemplateKey(docs), pos);


		 Paper pageSize = t.getPageSize();

		 MergeConfig mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);

		 PdfTask task = new PdfTask(this.parent, docs, pos, pageSize, mc, cc);
		 task.run();
	 }









































	 private boolean checkPrecondition()
	 {
		 if (PrintCil.isTraining()) {
			 MessageDialog.showErrorDlg(this.parent, null, NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000918"));
			 return false;
		 }

		 if (this.documents != null) {
			 return true;
		 }
		 if ((getTemplateID() == null) || (getTemplateID().trim().length() == 0)) {
			 MessageDialog.showErrorDlg(this.parent, null, NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000242") + "," + NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000243"));
			 return false;
		 }


		 if ((this.m_outputTask != null) && (this.m_outputTask.isCancel())) {
			 return false;
		 }
		 return true;
	 }




	 public PrintTempletmanageHeaderVO[] getAllTemplates()
	 {
		 return this.m_printDataAccessor.getAllTemplates();
	 }

	 public TemplateInfo getTemplate() {
		 return this.m_printDataAccessor.getPrintData().getTemplateInfo();
	 }

	 public int dsCountInPool() {
		 return this.m_dsOrganizer.getDataSourceCount();
	 }

	 public int getBatchExportType() {
		 return this.batchExportType;
	 }

	 public void setBatchExportType(int batchExportType) {
		 this.batchExportType = batchExportType;
	 }






	 public List<String> getUsedDataSourceVars()
	 {
		 if ((this.m_printDataAccessor instanceof DefaultTemplateDataAccessor)) {
			 PrintVO vo = ((DefaultTemplateDataAccessor)this.m_printDataAccessor).getOldPrintVO();

			 List<String> vars = new ArrayList();

			 if (vo == null) {
				 return vars;
			 }
			 if ((vo.getDatasourceVOs() == null) || (vo.getDatasourceVOs().length == 0)) {
				 return vars;
			 }
			 if ((vo.getCellVOs() == null) || (vo.getCellVOs().length == 0)) {
				 return vars;
			 }




			 String strvars = null;
			 for (PrintDatasourceVO dsVO : vo.getDatasourceVOs()) {
				 for (PrintCellVO cellVO : vo.getCellVOs()) {
					 if ((cellVO != null) && (dsVO != null) && (cellVO.getCcellcode() != null) && (dsVO.getCcellcode() != null) && (cellVO.getCcellcode().trim().equals(dsVO.getCcellcode().trim())))
					 {
						 strvars = cellVO.getVvar();

						 if (strvars != null)
						 {

							 String[] tmpVars = strvars.split("&");
							 for (String tmpVar : tmpVars)
								 if ((tmpVar != null) && ((!tmpVar.startsWith("\"")) || (!tmpVar.endsWith("\""))))
									 vars.add(tmpVar);
						 }
					 }
				 }
			 }
			 return vars;
		 }


		 return new ArrayList();
	 }







	 public List<String> getUsedDataSourceVars(String templateId)
	 {
		 setTemplateID(templateId);
		 return getUsedDataSourceVars();
	 }






	 public PrintSettingPanel getPrintSettingPanel()
	 {
		 TemplateInfo templateInfo = this.m_printDataAccessor.getPrintData().getTemplateInfo();
		 int dsCount = this.m_dsOrganizer.getDataSourceCount();
		 int printmodal = 1;
		 if (this.m_dsOrganizer.getDataSourceCount() > 1)
			 printmodal = 2;
		 PrintSettingPanel panel = new PrintSettingPanel(null, templateInfo, printmodal);
		 return panel;
	 }

	 public DocumentContainer getDocuments() {
		 return this.documents;
	 }

	 public void setTemplates(TemplateContainer templates) {
		 this.templates = templates;
	 }

	 public void setDocuments(DocumentContainer documents) {
		 this.documents = documents;
	 }






	 public String copyTemplate(String srcTemplateId, String targetNode)
			 throws BusinessException
			 {
		 PrintVO pv = PrintTemplateCache.getPrintTemplate(srcTemplateId);
		 pv.getTemplateVO().setPrimaryKey(null);
		 pv.getTemplateVO().setVnodecode(targetNode);
		 return PrintTemplateHelper.insert(pv);
			 }




	 private Template translateTemplate(ITemplateDataAccessor t)
	 {
		 return TemplateAdapter.translate((DefaultTemplateDataAccessor)this.m_printDataAccessor);
	 }

	 private OutputDocument organizeDocument() {
		 Template t = translateTemplate(this.m_printDataAccessor);
		 Paper pageSize = t.getPageSize();
		 MergeConfig mc = new MergeConfig(1, 1, 0.0D, 0.0D);

		 OutputDocument docs = new OutputDocument();
		 IDataSource oldDs = this.m_dsOrganizer.fetchNextDataSource();
		 int i = 0;
		 while (oldDs != null)
		 {
			 docs.addDoc(NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000111", null, new String[] { String.valueOf(i) }), NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000111", null, new String[] { String.valueOf(i) }), t, new OldDsAdapter(oldDs), 1);

			 i++;
			 oldDs = this.m_dsOrganizer.fetchNextDataSource();
		 }

		 return docs;
	 }











	 /**
	  * @deprecated
	  */
	 public nc.ui.pub.print.version55.print.output.paint.print.printset.PrintSettingDlg getPrintOutputSettingDlg(Container parent, PrintOutputSetting pos, Paper paper)
	 {
		 return new nc.ui.pub.print.version55.print.output.paint.print.printset.PrintSettingDlg(parent, pos, paper);
	 }




	 /**
	  * @deprecated
	  */
	 public Paper getTemplatePaper(String templateid)
			 throws PrintException
			 {
		 DefaultTemplateDataAccessor template = new DefaultTemplateDataAccessor();
		 try {
			 template.setTemplateID(templateid, true);
		 } catch (Exception e) {
			 throw new PrintException(e, e.getMessage());
		 }

		 Template t = TemplateAdapter.translate(template);

		 return t.getOutPageSize();
			 }



	 /**
	  * @deprecated
	  */
	 public ITemplateDataAccessor showTemplateSelectDlg(Container parent, String pkCorp, String funCode, String pkUser, String pkBusiType, String nodeKey, String orgtype)
	 {
		 PrintEntry pe = new PrintEntry(parent);

		 pe.setTemplateID(pkCorp, funCode, pkUser, pkBusiType, nodeKey, orgtype);

		 if (pe.selectTemplate() == -1) {
			 return null;
		 }

		 return pe.m_printDataAccessor;
	 }








	 /**
	  * @deprecated
	  */
	 public void print(IDataSource ds, ITemplateDataAccessor template, PrintOutputSetting pos, Paper cusPaper)
	 {
		 if (PrintCil.isTraining()) {
			 MessageDialog.showErrorDlg(this.parent, null, NCLangRes.getInstance().getStrByID("10100108", "UPP10100108-000918"));
			 return;
		 }

		 if ((ds == null) || (template == null)) {
			 return;
		 }
		 DefaultTemplateDataAccessor m_printDataAccessor = (DefaultTemplateDataAccessor)template;
		 IDataSourceOrganizer m_dsOrganizer = new DefaultDataSourceOrganizer();
		 m_dsOrganizer.addDataSource(ds);


		 OutputDocument docs = DocGroupUtil.orgOutputDocument(m_printDataAccessor, m_dsOrganizer);


		 Template t = TemplateAdapter.translate(m_printDataAccessor);

		 Paper paper = cusPaper;
		 if (paper == null) {
			 paper = t.getPageSize();
		 }

		 if (pos == null) {
			 pos = new PrintOutputSetting();
		 }
		 MergeConfig mc = TemplateAdapter.translateMergeConfig(m_printDataAccessor);
		 CopyConfig cc = TemplateAdapter.translateCopyConfig(m_printDataAccessor);
		 try
		 {
			 nc.ui.pub.print.version55.print.output.paint.print.PrintTask task = new nc.ui.pub.print.version55.print.output.paint.print.PrintTask(this.parent, docs, paper, mc, cc);
			 task.setListener(getPrintListener());
			 task.setPos(pos);
			 task.run();
		 } catch (PrintException e) {
			 PrintLog.error(e.getMessage(), e);
			 MessageDialog.showErrorDlg(this.parent, NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000056"), e.getMessage());
		 }
	 }





	 /**
	  * @deprecated
	  */
	 public void print(PrintOutputSetting pos, boolean isShowProgressDlg)
	 {
		 print(pos, null, isShowProgressDlg);
	 }









	 /**
	  * @deprecated
	  */
	 public void print(PrintOutputSetting pos, Paper cusPaper, boolean isShowProgressDlg)
	 {
		 if (!checkPrecondition()) {
			 return;
		 }

		 Template t = TemplateAdapter.translate((DefaultTemplateDataAccessor)this.m_printDataAccessor);


		 Paper paper = t.getPageSize();

		 if (cusPaper != null) {
			 paper = cusPaper;
		 }
		 OutputDocument docs = DocGroupUtil.orgOutputDocument((DefaultTemplateDataAccessor)this.m_printDataAccessor, this.m_dsOrganizer);


		 if (pos == null) {
			 pos = new PrintOutputSetting();
		 }
		 MergeConfig mc = TemplateAdapter.translateMergeConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 CopyConfig cc = TemplateAdapter.translateCopyConfig((DefaultTemplateDataAccessor)this.m_printDataAccessor);
		 try
		 {
			 nc.ui.pub.print.version55.print.output.paint.print.PrintTask task = new nc.ui.pub.print.version55.print.output.paint.print.PrintTask(this.parent, docs, paper, mc, cc);
			 task.setListener(getPrintListener());
			 task.setPos(pos);
			 if (isShowProgressDlg) {
				 task.setShowProgressor(true);
			 } else {
				 task.setShowProgressor(false);
			 }
			 task.run();


		 }
		 catch (PrintException e)
		 {


			 PrintLog.error(e.getMessage(), e);
			 MessageDialog.showErrorDlg(this.parent, NCLangRes.getInstance().getStrByID("10100108168", "10100108168-000056"), e.getMessage());
		 }
	 }



	 public void clearDataSource() {
		 this.m_dsOrganizer.clear();
	 }


	public PrintOutputSetting getDevelopPos() {
		return developPos;
	}


	public void setDevelopPos(PrintOutputSetting developPos) {
		this.developPos = developPos;
	}


	public Paper getDevelopPaper() {
		return developPaper;
	}


	public void setDevelopPaper(Paper developPaper) {
		this.developPaper = developPaper;
	}
	 
}